<?php
/**
 * classe File
 *
 *
 * PHP version 5
 *
 * LICENSE: GNU General Public License v3
 *
 * @author     Poly Charly <CharlyPoly@gmail.fr>
 * @copyright  2009 Authentic Development Group
 * @license    GNU General Public License v3
 * @todo       Implement word and pdf reader...
 *
 **/
class file {
    /*
     * @var string
     */
    public  $type           =   'application/octet-stream';
    /*
     * @var string
     */
    private $filePath;
    /*
     * @var boolean
     */
    private $useCache       =   true;
    /*
     * @var string
     */
    public  $lock           =   true;
    /*
     * @var boolean
     */
    public  $WriteControl   =   false;
    /*
     * @var boolean
     */
    public  $ReadControl    =   false;
    /*
     * @var string
     */
    private $cache          =   false;


    function __construct($filePath, $args=NULL) {
        $this->filePath     = $filePath;
        $this->type         = $this->type();
        $this->lock         = (isset($args['lock']) && is_bool($args['lock'])) ? $args['lock'] : TRUE;
        $this->ReadControl  = (isset($args['ReadControl']) && is_bool($args['ReadControl'])) ? $args['ReadControl'] : FALSE;
        $this->WriteControl = (isset($args['WriteControl']) && is_bool($args['WriteControl'])) ? $args['WriteControl'] : FALSE;
        $this->useCache     = (isset($args['useCache']) && is_bool($args['useCache'])) ? $args['useCache'] : TRUE;
    }
    /*
     * @return mixed
     */
    public function read($XmlParse = TRUE) {
		if ($this->type == 'xml' && ($XmlParse == TRUE)) {
            if (function_exists('simple_xml_load_file')) {
                $cache = new Cache_Memory(md5('File-xml'.$this->filePath), '/cache');
                if ($xmlVar = $cache->get()) {
                    return $xmlVar ;
                } else {
                    $xml = simplexml_load_file($this->filePath);
                    return $cache->save($xml);
                }
            }
		} elseif ($this->type == 'pdf') {
            //read pdf
            //create cache MEMORY
		} elseif ($this->type == 'doc') {
            //read fichier word
            //create cache MEMORY
		} else {
            if (!$this->cache) {
                $handle = fopen($this->filePath, "r");
                $contents = fread($handle, filesize($this->filePath));
                $this->cache = $contents;
                $return = $this->cache;
                if ($this->ReadControl) {
                    $return = (strlen($contents) == filesize($this->filePath)) ? $this->cache : false;
                }
                fclose($handle);
                return $return;
            } else {
                return $this->cache;
            }
        }
	}
    /*
     * @return mixed
     */
    public function write($content, $args=NULL) {
        if ((file_exists($this->filePath) && ($args['erase'] == TRUE)) || !file_exists($this->filePath)) {
            if ($args['lock']) $tmpName = tempnam(TMP_DIR, "FILE");
            $this->cache = file_put_contents( ($args['lock']) ? tempnam : $this->filePath  , $content , ($args['append']) ? FILE_APPEND : '' );
            if (($args['lock']) && ($this->cache !== FALSE)) rename($tempnam ,$this->filePath);
            return ($this->WriteControl) ? true :($writed == strlen($this->cache));
        } else {
            Error::getInstance(E_ERROR ,'can\'t erase file !', __FILE__, __LINE__);
        }
    }
    /*
     * @return string
     */
	public function modifiedDate($mask = "d/m/y,  h:m:i") {
		if (file_exists($this->filePath)) {
            $fmt = @filemtime($this->filePath) or die('Fail Read Create Time File');
            return date($mask, $fmt);
        }
	}
    /*
     * @return string
     */
    public function accessDate($mask = "d/m/y,  h:m:i") {
        if (file_exists($this->filePath)) {
            $fmt = @fileatime($this->filePath) or die('Fail Read Create Time File');
            return date($mask, $fmt);
        }
	}

    /*
     * @return object
     */
    public function stats() {
        $arg = stat ( $this->filePath );
        $return = (object) NULL;
        foreach ($arg as $k=>$v) {
            $return->{$k} = $v;
        }
        return $return;
    }

    function delete() {
        return @unlink($this->filePath);
    }

    private function type() {
        $mime_types = array(

                'txt' => 'text/plain',
                'htm' => 'text/html',
                'html' => 'text/html',
                'php' => 'text/html',
                'css' => 'text/css',
                'js' => 'application/javascript',
                'json' => 'application/json',
                'xml' => 'application/xml',
                'swf' => 'application/x-shockwave-flash',
                'flv' => 'video/x-flv',

                // images
                'png' => 'image/png',
                'jpe' => 'image/jpeg',
                'jpeg' => 'image/jpeg',
                'jpg' => 'image/jpeg',
                'gif' => 'image/gif',
                'bmp' => 'image/bmp',
                'ico' => 'image/vnd.microsoft.icon',
                'tiff' => 'image/tiff',
                'tif' => 'image/tiff',
                'svg' => 'image/svg+xml',
                'svgz' => 'image/svg+xml',

                // archives
                'zip' => 'application/zip',
                'rar' => 'application/x-rar-compressed',
                'exe' => 'application/x-msdownload',
                'msi' => 'application/x-msdownload',
                'cab' => 'application/vnd.ms-cab-compressed',

                // audio/video
                'mp3' => 'audio/mpeg',
                'qt' => 'video/quicktime',
                'mov' => 'video/quicktime',

                // adobe
                'pdf' => 'application/pdf',
                'psd' => 'image/vnd.adobe.photoshop',
                'ai' => 'application/postscript',
                'eps' => 'application/postscript',
                'ps' => 'application/postscript',

                // ms office
                'doc' => 'application/msword',
                'rtf' => 'application/rtf',
                'xls' => 'application/vnd.ms-excel',
                'ppt' => 'application/vnd.ms-powerpoint',

                // open office
                'odt' => 'application/vnd.oasis.opendocument.text',
                'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
            );

            $ext = strtolower(array_pop(explode('.',$this->filePath)));
            if (array_key_exists($ext, $mime_types)) {
                return $mime_types[$ext];
            }
            elseif (function_exists('finfo_open')) {
                $finfo = finfo_open(FILEINFO_MIME);
                $mimetype = finfo_file($finfo, $this->filePath);
                finfo_close($finfo);
                return $mimetype;
            }
            else {
                return 'application/octet-stream';
            }
       }// Type End
}
?>